import request from "@/utils/request";
const LOCATION_BASE_URL = "/locations";

const LocationAPI = {
  getList() {
    return request<any, LocationVO[]>({
      url: `${LOCATION_BASE_URL}` + "/list",
      method: "get",
    });
  },

  getOptions(onlyParent?: boolean) {
    return request<any, OptionType[]>({
      url: `${LOCATION_BASE_URL}/getAllLocationsTree`,
      method: "get",
      params: { onlyParent: onlyParent },
    });
  },

  getFormData(id: number) {
    return request<any, LocationForm>({
      url: `${LOCATION_BASE_URL}/getLocationById/${id}`,
      method: "get",
    });
  },

  add(data: LocationForm) {
    return request({
      url: `${LOCATION_BASE_URL}` + "/create",
      method: "post",
      data: data,
    });
  },

  update(id: number, data: LocationForm) {
    return request({
      url: `${LOCATION_BASE_URL}/update`,
      method: "post",
      data: data,
    });
  },

  updateForbidStatus(id: number, isForbid: number) {
    return request({
      url: `${LOCATION_BASE_URL}/updateForbidStatus/${id}/${isForbid}`,
      method: "get",
    });
  },

  setDevices(localId: number, data: any) {
    return request({
      url: `${LOCATION_BASE_URL}/setDevices/${localId}`,
      method: "post",
      data: data,
    });
  },

  deleteById(id: number) {
    return request({
      url: `${LOCATION_BASE_URL}/delete/${id}`,
      method: "get",
    });
  },

  getTree(queryParams: any) {
    return request<any, any>({
      url: `${LOCATION_BASE_URL}/getTreeJson`,
      method: "post",
      data: queryParams,
    });
  },
  getRealLocations(queryParams: any) {
    return request<any, any>({
      url: `${LOCATION_BASE_URL}/getRealLocations`,
      method: "post",
      data: queryParams,
    });
  },

  getParentIsForbid(id: number) {
    return request<any, any>({
      url: `${LOCATION_BASE_URL}/getParentIsForbid/${id}`,
      method: "get",
    });
  },
};

export default LocationAPI;

export interface LocationVO {
  children?: LocationVO[];
  localId?: number;
  parentId?: number;
  isForbid?: number;
  name?: string;
  remark?: string;
  sort?: number;
  droppedData: [];
  isDragOver: boolean;
}

export interface LocationForm {
  localId?: number;
  parentId?: number;
  isForbid?: number;
  name?: string;
  remark?: string;
  sort?: number;
}

export interface LocationTree {
  id: number;
  label: string;
  children?: LocationTree[];
}
